package com.likai.authority.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.likai.authority.pojo.User;

public class LoginInterceptor implements HandlerInterceptor {
	
	public static final String [] IGNORE_URL = {"loginJsp","login"} ;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean flag = false ;
		String path = request.getServletPath() ;
		for (String url : IGNORE_URL) {
			if(path.contains(url)) {
				flag = true ;
				break ;
			}
		}
		if(!flag) {
			User user = (User) request.getSession().getAttribute("user") ;
			if(user == null) {
				request.getRequestDispatcher("loginJsp").forward(request, response);
				return flag ;
			} else {
				flag = true ;
			}
		}
		return flag;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
